/*固定定时器任务
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.tcp;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.Map;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.business.Analyse_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.db1.DBManager1;
import com.zxy.db10.DBManager10;
import com.zxy.db2.DBManager2;
import com.zxy.db3.DBManager3;
import com.zxy.db4.DBManager4;
import com.zxy.db5.DBManager5;
import com.zxy.db6.DBManager6;
import com.zxy.db7.DBManager7;
import com.zxy.db8.DBManager8;
import com.zxy.db9.DBManager9;
import com.zxy.db_Self.DBManager_Self;

public class SelfTimeTask extends java.util.TimerTask
{
	@Override
	public void run()
	{
		RunTask();
	}

	private boolean RunTaskFlag()
	{
		if(1 == 1)
			return true;
		if(Com_Para.strSessionAva.equals(""))
			return false;
		String strF = Com_Fun.getSconnections(Com_Para.strSessionAva);
		long lgAva = Long.valueOf(strF.substring(32,45));
		long lgTime = new Date().getTime() - lgAva;
		if(lgTime < 100 * 24 * 60 * 60 * 1000L)// 有效期100天
			return true;
		else
		{
			System.out.println("超过系统试用版本时间,请联系开发单位");
			return false;
		}
	}

	private void DbTimeOut()
	{
		// if(!DBManager_Self.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
		DBManager_Self.closeConnectionPoolTimeOut();
		if(DBManager1.bUse && DBManager1.inst != null && !DBManager1.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager1.closeConnectionPoolTimeOut();
		if(DBManager2.bUse && DBManager2.inst != null && !DBManager2.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager2.closeConnectionPoolTimeOut();
		if(DBManager3.bUse && DBManager3.inst != null && !DBManager3.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager3.closeConnectionPoolTimeOut();
		if(DBManager4.bUse && DBManager4.inst != null && !DBManager4.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager4.closeConnectionPoolTimeOut();
		if(DBManager5.bUse && DBManager5.inst != null && !DBManager5.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager5.closeConnectionPoolTimeOut();
		if(DBManager6.bUse && DBManager6.inst != null && !DBManager6.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager6.closeConnectionPoolTimeOut();
		if(DBManager7.bUse && DBManager7.inst != null && !DBManager7.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager7.closeConnectionPoolTimeOut();
		if(DBManager8.bUse && DBManager8.inst != null && !DBManager8.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager8.closeConnectionPoolTimeOut();
		if(DBManager9.bUse && DBManager9.inst != null && !DBManager9.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager9.closeConnectionPoolTimeOut();
		if(DBManager10.bUse && DBManager10.inst != null && !DBManager10.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager10.closeConnectionPoolTimeOut();
	}

	private void RunTask()
	{
		if(!Com_Para.bTimeFlag && RunTaskFlag())
		{
			Com_Para.bTimeFlag = true;
			Com_Para.iTimeCount2++;
			try
			{
				// 判断session
				SetSession();
				// 往服务端发送数据包
				SendInfo();
				// 往客户端发送数据包
				SendClientInfo();
				// DB连接超时
				DbTimeOut();
				// 删除过期数据文件
				DelFileInfo();
				// 检测连接的子设备连接数
				CheckServerSocket();
			}
			catch(Exception ee)
			{
			}
			Com_Para.bTimeFlag = false;
		}
	}

	private void SetSession()
	{
		if(Com_Para.iTimeCount2 % 2 == 0)
		{
			if(Com_Para.iTimeCount2 >= 10)
				Com_Para.iTimeCount2 = 0;
			if(Com_Para.htSession != null)
			{
				for(Map.Entry<String,Object[]> entry: Com_Para.htSession.entrySet())
				{
					Object[] objV = entry.getValue();
					Date dtSession = new Date();
					try
					{
						dtSession = (Date)objV[1];
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
					long lgTime = new Date().getTime() - dtSession.getTime();
					if(lgTime > Com_Para.SessionTimeOut * 60000)// session有效判断7天
					{
						Com_Para.htSession.remove(entry.getKey());
					}
				}
			}
		}
	}

	private void CheckServerSocket()
	{
		if(Com_Para.iTimeCount2 % 2 == 0)
		{
			if(Com_Para.htServSockets != null)
			{
				for(Map.Entry<String,Socket> entry: Com_Para.htServSockets.entrySet())
				{
					Socket temSck = entry.getValue();
					// UsAdmin_Log uL = new
					// UsAdmin_Log(Com_Para.ApplicationPath,new
					// StringBuilder("Socket集合:"
					// + entry.getKey()
					// + " "
					// + temSck.getInetAddress().getHostAddress()
					// + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
					// try
					// {
					// uL.WriteLogSub("Socket");
					// }
					// catch(IOException e)
					// {
					// }
				}
			}
		}
	}

	private void SendClientInfo()
	{
		if(Com_Para.htServSockets != null && Com_Para.iTimeCount2 % 5 == 0)
		{
			for(Map.Entry<String,Socket> entry: Com_Para.htServSockets.entrySet())
			{
				Socket temSck = entry.getValue();
				// 发送数据包
				try
				{
					if(entry.getKey().indexOf("servSocket") != -1)
					{
						Analyse_Data ad = new Analyse_Data();
						ad.SendSocket(new StringBuilder("*\r\n"),temSck);

						if(Com_Para.htServSockets.get("servSocket" + temSck.getInetAddress().getHostAddress()) == null)
							Com_Para.htServSockets.put("servSocket" + temSck.getInetAddress().getHostAddress(),temSck);
					}
				}
				catch(Exception se)
				{
					UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("网络异常:往客户端发送心跳包异常;" + se.getMessage() + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
					try
					{
						uL.WriteLogSub("NetWork");
					}
					catch(IOException e)
					{
					}
					Com_Para.htServSockets.remove("servSocket" + temSck.getInetAddress().getHostAddress());
					temSck = null;
				}
			}
		}
	}

	private void SendInfo()
	{
		// 发送数据包
		if(Com_Para.clientSocket != null)
		{
			try
			{
				Analyse_Data ad = new Analyse_Data();
				ad.SendSocket(new StringBuilder("*"),Com_Para.clientSocket);
			}
			catch(Exception se)
			{
				UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("网络异常:往服务端发送心跳包网络断开;" + se.getMessage() + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
				try
				{
					uL.WriteLogSub("NetWork");
				}
				catch(IOException e)
				{
				}
			}
		}
	}

	@SuppressWarnings("deprecation")
	private void DelFileInfo()
	{
		// 删除数据文件
		if(new Date().getHours() == 2 && new Date().getMinutes() % 30 == 0)
		{
			String FilePath = Com_Para.ApplicationPath + Com_Para.zxyPath + "data";
			File fiFile = new File(FilePath);
			if(fiFile.exists() && fiFile.isDirectory())
			{
				File[] fils = fiFile.listFiles();
				for(File fln: fils)
				{
					Date dtNow = new Date();
					dtNow.setDate(dtNow.getDate() - 1);// 1天前
					if(dtNow.compareTo(new Date(fln.lastModified())) == 1)
					{
						fln.delete();
					}
				}
			}
		}
	}
}